package com.alphaking.util;


import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtil {
	private static DateFormat format = null;
    private static DateFormat dayForm  =null;
    private static DateFormat timeForm = null;
    private static DateFormat chineseDayForm = null;
    private static DateFormat chineseForm = null;
    private static DateFormat idDayForm = null;
	static {
		format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		timeForm = new SimpleDateFormat("HH:mm:ss");
		dayForm = new SimpleDateFormat("yyyy-MM-dd");
		chineseDayForm = new SimpleDateFormat("yyyy年MM月dd日");
		chineseForm = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
		idDayForm = new SimpleDateFormat("yyMMdd");
	}

	public static String timeStamp2DateStr(Timestamp timestamp) {
		if(timestamp==null){
			return "";
		}

		String dateStr = format.format(timestamp);
	//	System.out.println(dateStr);
		return dateStr;

	}

	public static String timeStamp2dayStr(Timestamp timestamp) {
		if(timestamp==null){
			return "";
		}
		String dayStr =dayForm.format(timestamp);
		//System.out.println(dayStr);
		return dayStr;
	}
	
	public static Timestamp dayStr2timeStamp(String dayStr) {
		if(dayStr==null){
			return null;
		}
		Timestamp t = null;
		try {
			t = new Timestamp(dayForm.parse(dayStr).getTime());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//System.out.println(dayStr);
		return t;
	}
	
	public static Timestamp secondStr2timeStamp(String secondStr) {
		if(secondStr==null){
			return null;
		}
		Timestamp t = null;
		try {
			t = new Timestamp(format.parse(secondStr).getTime());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//System.out.println(dayStr);
		return t;
	}
	
	public static Date dayStr2Date(String dayStr) {
		if(dayStr==null){
			return null;
		}
		Date d = null;
		try {
			d = dayForm.parse(dayStr);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//System.out.println(dayStr);
		return d;
	}

	public static String timeStamp2TimeStr(Timestamp timestamp) {
		if(timestamp==null){
			return "";
		}
		String timeStr = timeForm.format(timestamp);
		//System.out.println(timeStr);
		return timeStr;
	}
	
	public static String timeStamp2ChineseDayStr(Timestamp timestamp) {
		if(timestamp==null){
			return "";
		}
		String timeStr = chineseDayForm.format(timestamp);
		System.out.println(timeStr);
		return timeStr;
	}
	public static String timeStamp2ChineseFormStr(Timestamp timestamp) {
		if(timestamp==null){
			return "";
		}
		String timeStr = chineseForm.format(timestamp);
		System.out.println(timeStr);
		return timeStr;
	}
	
	
	
	
	
	public static String date2DateStr(Date date) {
		if(date==null){
			return "";
		}
		String dateStr = format.format(date);
	//	System.out.println(dateStr);
		return dateStr;

	}

	public static String date2dayStr(Date date) {
		if(date==null){
			return "";
		}
		String dayStr =dayForm.format(date);
		//System.out.println(dayStr);
		return dayStr;
	}

	public static String date2IdDayStr(Date date) {
		if(date==null){
			return "";
		}
		String dayStr =idDayForm.format(date);
		//System.out.println(dayStr);
		return dayStr;
	}
	public static String date2TimeStr(Date date) {
		if(date==null){
			return "";
		}
		String timeStr = timeForm.format(date);
		//System.out.println(timeStr);
		return timeStr;
	}
	
	public static String date2ChineseDayStr(Date date) {
		if(date==null){
			return "";
		}
		String timeStr = chineseDayForm.format(date);
		System.out.println(timeStr);
		return timeStr;
	}
	public static String date2ChineseFormStr(Date date) {
		if(date==null){
			return "";
		}
		String timeStr = chineseForm.format(date);
		System.out.println(timeStr);
		return timeStr;
	}
	
	//保留timeStamp日期
	public static Timestamp timeStamp2DateStamp(Timestamp timeStamp) {
		Timestamp time = null;
		if(timeStamp==null){
			time = null;
		}else{
			try {
				String sdate = dayForm.format(timeStamp);
				Date date = dayForm.parse(sdate);
				time = new Timestamp(date.getTime());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return time;
	}
	
	public static Timestamp getNowTime() {
		Timestamp timestamp = new Timestamp(System.currentTimeMillis());
		return timestamp;
	}
	public static String getNowTimeStr() {
		String str = DateUtil.timeStamp2DateStr(new Timestamp(System.currentTimeMillis()));
		return str;
	}
	public static String getNowDayStr() {
		String str = DateUtil.date2IdDayStr(new Timestamp(System.currentTimeMillis()));
		return str;
	}
	
	public static void main(String[] args) {
/*		timeStamp2DateStr(new Timestamp(System.currentTimeMillis()));
		timeStamp2dayStr(new Timestamp(System.currentTimeMillis()));
		timeStamp2TimeStr(new Timestamp(System.currentTimeMillis()));
		timeStamp2ChineseDayStr(new Timestamp(System.currentTimeMillis()));
		timeStamp2ChineseFormStr(new Timestamp(System.currentTimeMillis()));*/
		System.out.println(date2IdDayStr(new Date()));
	}
}
